global class InsertRole_Template{

   WebService static String insertRecord(String roleId, String templateId)
   {
     System.LoggingLevel level = LoggingLevel.DEBUG;

     Template_Role__c[] tRole = [SELECT Role_ID__c, Template_ID__c  from Template_Role__c t where t.Role_ID__c= :roleId and t.Template_ID__c = :templateId];

     if(tRole.size() > 0)
     {
        return 'Record already exists';
     }
     
     Template__c[] template = [select Id, Name, EligibilityType__c from Template__c where Id = :templateId];
     if(template.size() > 0)
     {
         template[0].EligibilityType__c = 'Role';
     }
     try
     {
         update template;
     }
     catch(Exception e)
     {}
     
     UserRole[] ur = [select Name,RollupDescription from UserRole where Id = :roleId];
     if(ur.size() > 0)
     {
         Template_Role__c tempRole = new Template_Role__c(Role_ID__c = roleId, Template_ID__c = templateId, Template__c = templateId, Role_Name__c = ur[0].Name, Description__c = ur[0].RollupDescription);
         try
         {
           insert tempRole;
         }
         catch(DmlException e)
         {}
     }
     return 'Record saved successfully';
   }

   WebService static String insertTitleRecord(String title, String templateId){
   
     System.LoggingLevel level = LoggingLevel.DEBUG;

     Template_Role__c[] tRole = [SELECT Role_ID__c, Template_ID__c  from Template_Role__c t where t.User_Title__c= :title and t.Template_ID__c = :templateId];

     if(tRole.size() > 0)
     {
        return 'Record already exists';
     }
     
     Template__c[] template = [select Id, EligibilityType__c from Template__c where Id = :templateId];
     if(template.size() > 0)
     {
         template[0].EligibilityType__c = 'Title';
     }
     try
     {
         update template;
     }
     catch(Exception e)
     {
     }
     
     Template_Role__c tempRole = new Template_Role__c(Template_ID__c = templateId, Template__c = templateId, User_Title__c = title);
     try
     {
       insert tempRole;
     }
     catch(DmlException e)
     {
       
     }
     return 'Record saved successfully';
   }//end of insertTitleRecord()

    WebService static String deleteRole(String templateId, String RoleId){
   
        Template_Role__c[] tRole = [SELECT Id from Template_Role__c t where t.Id= :RoleId and t.Template_ID__c = :templateId];  
        if(tRole.size() > 0)
        {
            try
            {
                if(tRole[0]!=null)
                    delete tRole[0];
            }
            catch(DmlException d)
            {
                return ' '+d;
            }
        }
        return 'Deleted Successfully';
    }
   
   Webservice static String insertUrl(String url)
   {
       URL__c u = new URL__c();
       
       u.Url_Value__c = url;
       try
       {
           insert u;
       }
       catch(DmlException d)
       {   
           return d+'';
       }
       return 'success'; 
   }
   
    Webservice static String[] getTitles()
    {
        String[] titles = new List<String>();
        Map<String,String> checkTitle = new Map<String, String>();

        try
        {
            for(User[] users: [select Title from User where Title != '' order by Title])
            {
                for(User u:users)
                {
                    if(checkTitle.get(u.Title) == null)
                    {
                        checkTitle.put(u.Title, u.Title);
                        titles.add(u.Title);

                    }
                }
            }
        }
        catch(Exception e)
        {
            //return e.getMessage();
        }
        
        return titles;
    }//end of getTitles()

static testMethod void testCase() 
{
    String roleId = '00E40000000ju5rEAA';
    String templateId ='a0D40000002XnAT';
    
    insertRecord('00E40000000ju5rEAA','a0D400000020eAQ');//(roleId, templateId)
    insertRecord('00E40000000ju5rEAA','a0D40000002XnAT');//(roleId, templateId)
    
    insertTitleRecord('Account Executive East', 'a0D40000001mHXT');//(title, templateId)
    insertTitleRecord('Account Executive','a0D40000002XnAU');//(title, templateId)
    
    deleteRole('00E40000000ju5rEAA','a0D40000002XnAT');
    insertUrl('http://na2.salesforce.com');
    String[] titles = getTitles();
}


}